import { queryProfile, queryFriends,queryChargeLog,queryMoneyLog,queryChangePoint,queryPointLog,queryChangeMoney } from '../services/user';
import {message} from 'antd'

export default {
  namespace: 'profile',

  state: {
    basicGoods: [],
    basicLoading: true,
    profileLoading: true,
    chargeLogs: {pagination:{current:1,size:10,total:0},list:[]},
    moneyLogs: {pagination:{current:1,size:10,total:0},list:[]},
    pointLogs: {pagination:{current:1,size:10,total:0},list:[]},
      friends:[],
    advancedLoading: true,
    confirmLoading: false,
      friends_loading:false,
    pointAddShow:false,
    moneyAddShow:false,
    user:{ info:{}},
  },

  effects: {
    *fetchProfile({payload,callback}, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { profileLoading: true },
      });
      const response = yield call(queryProfile,payload);

      yield put({
        type: 'show',
        payload: {user:response.data},
      });
        yield put({
            type: 'fetchFriends',
            payload: {user_id:payload.id},
        });
      yield put({
        type: 'changeLoading',
        payload: { profileLoading: false },
      });
    },
      *fetchFriends({payload,callback}, { call, put }) {
          yield put({
              type: 'changeLoading',
              payload: { friends_loading: true },
          });
          const response = yield call(queryFriends,payload);
          yield put({
              type: 'show',
              payload: {friends:response.data},
          });
          yield put({
              type: 'changeLoading',
              payload: { friends_loading: false },
          });
      },
    *changePoint({payload,callback},{call,put}){
      yield put({
        type: 'changeLoading',
        payload: { confirmLoading: true },
      });
      const response = yield call(queryChangePoint,payload);
      if(response.status){
        message.success('修改成功');
        if(callback){
          callback();  
        }
      }else {
        message.error('修改失败');
      }
      
      yield put({
        type: 'changeLoading',
        payload: { confirmLoading: false },
      });
    },
    *changeMoney({payload,callback},{call,put}){
      yield put({
        type: 'changeLoading',
        payload: { confirmLoading: true },
      });
      const response = yield call(queryChangeMoney,payload);
      if(response.status){
        message.success('修改成功');
        if(callback){
          callback();  
        }
      }else {
        message.error('修改失败');
      }
      
      yield put({
        type: 'changeLoading',
        payload: { confirmLoading: false },
      });
    },

    *fetchCharge({payload,callbcak}, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { advancedLoading: true },
      });
      const response = yield call(queryChargeLog,payload);
      yield put({
        type: 'show',
        payload: {chargeLogs:{pagination:response.page,list:response.data}},
      });
      yield put({
        type: 'changeLoading',
        payload: { advancedLoading: false },
      });
    },
    *fetchPoint({payload,callbcak}, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { advancedLoading: true },
      });
      const response = yield call(queryPointLog,payload);
      yield put({
        type: 'show',
        payload: {pointLogs:{pagination:response.page,list:response.data}},
      });
      yield put({
        type: 'changeLoading',
        payload: { advancedLoading: false },
      });
    },
    *fetchMoney({payload,callbcak}, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: { advancedLoading: true },
      });
      const response = yield call(queryMoneyLog,payload);
      yield put({
        type: 'show',
        payload: {moneyLogs:{pagination:response.page,list:response.data}},
      });
      yield put({
        type: 'changeLoading',
        payload: { advancedLoading: false },
      });
    },
  },

  reducers: {
    show(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    changeModal(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    changeLoading(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
